package com.bq.gametrade.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.adapter.PlatformAccountAdapter;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.BindGameAccount;
import com.bq.gametrade.bean.BindGameAccountDetails;
import com.bq.gametrade.bean.UserInfo;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class PlatformInterconnectedActivity extends BaseActivity implements View.OnClickListener{

    private ImageView back,ivService;
    private TextView tvTitle;
    private ListView listView;

    private PlatformAccountAdapter adapter;
    private ArrayList<BindGameAccountDetails> mList = new ArrayList<>();

    private Activity activity;
    private LinearLayout layPrompt;
    private UserInfoDetails userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_platforminterconnected_list);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        activity = this;
        initView();
    }

    private void initView() {
        back = serchViewById(R.id.image_back);
        back.setOnClickListener(this);
        ivService = serchViewById(R.id.iv_userService);
        ivService.setVisibility(View.VISIBLE);
        ivService.setImageResource(R.mipmap.icon_add);
        ivService.setOnClickListener(this);
        tvTitle = serchViewById(R.id.title_name);
        tvTitle.setText("平台互联");
        listView = serchViewById(R.id.lv_platformAccount);
        adapter = new PlatformAccountAdapter(PlatformInterconnectedActivity.this,mList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(activity,GameAccountDetailsActivity.class);
                intent.putExtra("gameNameId",mList.get(position).id);
                intent.putExtra("gameName",mList.get(position).serverName);
                startActivity(intent);
            }
        });
        layPrompt = serchViewById(R.id.lay_prompt);

    }

    @Override
    protected void onResume() {
        super.onResume();
        getBindAccountList();

    }

    private void getBindAccountList(){
        Map<String, String> params = new HashMap<>();
        params.put("token",userInfo.token);
        showSpotsDialog();
        VRHttp.getBindGamesAccountList(PlatformInterconnectedActivity.this, HttpLink.BINDGAMESACCOUNT_LIST_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                closeSpotsDialog();
                if(response != null){
                    BindGameAccount bindGameAccount = (BindGameAccount)response;
                    if(bindGameAccount.status.equals("0")){
                        mList = bindGameAccount.gamelist;
                        if(mList.size()>0){
                            adapter.setData(mList);
                            adapter.notifyDataSetChanged();
                            layPrompt.setVisibility(View.GONE);
                        }else {
                            layPrompt.setVisibility(View.VISIBLE);
                        }

                    }else if (bindGameAccount.status.equals("99")) {
                        toastShow("登录超时");
                        application.setUserInfo(null);
                        startActivity(new Intent(activity, LoginActivity.class));
                        finish();
                    } else if (bindGameAccount.status.equals("98")) {
                        toastShow("您的账号已在其它设备登录，请重新登录修改您的密码");
                        application.setUserInfo(null);
                        startActivity(new Intent(activity, LoginActivity.class));
                        finish();
                    }


                }
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                closeSpotsDialog();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });
    }






    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_back:
                finish();
                break;
            case R.id.iv_userService:
                startActivity(new Intent(activity, BindingGamesAccountActivity.class));
                break;

        }
    }
}
